package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.query.UserQueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StrategyServiceImpl implements IStrategyService {
    @Autowired
    private StrategyMapper strategyMapper;

    /**
     * 根据关键字查询大攻略条数
     * @param qo
     * @return
     */
    @Override
    public int selectCountByKeyword(StrategyQueryObject qo) {
        return strategyMapper.selectCountByKeyword(qo);
    }

    @Override
    public Strategy getBystrategyId(Long id) {
        return strategyMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Strategy> listAll() {
        return strategyMapper.selectAll();
    }



    @Override
    public void saveOrUpdate(Strategy strategy) {
        if (strategy.getId() != null) {
            strategyMapper.updateByPrimaryKey(strategy);
        }else{
            strategyMapper.insert(strategy);
        }
    }


    @Override
    public PageInfo<Strategy> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),true,false,null);//禁用合理化分页
        PageHelper.orderBy(qo.getOrderBy());
        List<Strategy> list = strategyMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    /**
     * 通过用户的id查找收藏的游记
     * @param qo
     * @return
     */
    @Override
    public PageInfo selectReviewById(UserQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),true,false,null);//禁用合理化分页
        List<Strategy> list = strategyMapper.selectReviewById(qo.getId());
        return new PageInfo<>(list);
    }


    /**
     * 通过用户id和攻略的id;来删除对应的收藏关系
     * @param userId
     * @param strategyId
     */
    @Override
    public void deleteCollectByStrategyId(Long userId, Long strategyId) {
        strategyMapper.deleteCollectByStrategyId(userId,strategyId);
    }

    /**
     * 通过攻略的id来查找攻略收藏的用户的总条数
     * @param strategyId
     * @return
     */
    @Override
    public int selectCollectCountByStrategyId(Long strategyId) {
        int collectCount = strategyMapper.selectCollectCountByStrategyId(strategyId);
        return collectCount;
    }

    /**
     * 根据用户的id和攻略的id来查找攻略收藏的状态
     * @param userId
     * @param strategyId
     * @return
     */
    @Override
    public boolean selectCollectByStrategyId(Long userId, Long strategyId) {
        Map user = strategyMapper.selectCollectByStrategyId(userId, strategyId);
        if(user != null){
            return true;
        }
        return false;
    }

    /**
     * 通过攻略的id保存对应的收藏关系
     * @param userId
     * @param strategyId
     */
    @Override
    public void saveCollectByStrategyId(Long userId, Long strategyId) {
        strategyMapper.saveCollectByStrategyId(userId,strategyId);
    }

}
